package com.linksfield.topup.service.impl;

import com.alibaba.fastjson.JSON;
import com.linksfield.topup.config.dto.CalculateResultParamVo;
import com.linksfield.topup.config.dto.PageAndSort;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.common.*;
import com.linksfield.topup.entity.interfaces.planmanage.createserviceplan.request.RequestCreateServicePlan;
import com.linksfield.topup.entity.interfaces.planmanage.createserviceplan.request.RequestServicePlanSpecAndPriceList;
import com.linksfield.topup.entity.interfaces.planmanage.edit.request.RequestServicePlanEdit;
import com.linksfield.topup.entity.interfaces.planmanage.edit.response.ResponseEdit;
import com.linksfield.topup.entity.interfaces.planmanage.edit.response.ResponsePackageInfo;
import com.linksfield.topup.entity.interfaces.planmanage.edit.response.ResponseServicePlanInfo;
import com.linksfield.topup.entity.interfaces.planmanage.edit.response.ResponseServicePlanSpecPrice;
import com.linksfield.topup.entity.interfaces.planmanage.editpageoperate.request.RequestEditPageOperate;
import com.linksfield.topup.entity.interfaces.planmanage.languge.request.RequestPartnerLanguageType;
import com.linksfield.topup.entity.interfaces.planmanage.languge.response.LanguageType;
import com.linksfield.topup.entity.interfaces.planmanage.languge.response.ResponsePartnerLanguageType;
import com.linksfield.topup.entity.interfaces.planmanage.operate.request.RequestServicePlanOperate;
import com.linksfield.topup.entity.interfaces.planmanage.operate.request.RequestServicePlanOperateList;
import com.linksfield.topup.entity.interfaces.planmanage.packagebasicinfo.request.RequestPackageBasicInfo;
import com.linksfield.topup.entity.interfaces.planmanage.packagebasicinfo.response.ResponsePackageBasicInfo;
import com.linksfield.topup.entity.interfaces.planmanage.packagelist.request.RequestPackageNameList;
import com.linksfield.topup.entity.interfaces.planmanage.packagelist.response.ResponsePackageNameList;
import com.linksfield.topup.entity.interfaces.planmanage.partnernamelist.request.RequestPartnerNameList;
import com.linksfield.topup.entity.interfaces.planmanage.partnernamelist.response.ResponsePartnerNameList;
import com.linksfield.topup.entity.interfaces.planmanage.search.request.RequestServicePlanSearch;
import com.linksfield.topup.entity.interfaces.planmanage.search.response.ResponseCalculateCount;
import com.linksfield.topup.entity.interfaces.planmanage.search.response.ResponseSearch;
import com.linksfield.topup.entity.interfaces.planmanage.search.response.ResponseServicePlan;
import com.linksfield.topup.mapper.ServicePlanMapper;
import com.linksfield.topup.service.SequenceService;
import com.linksfield.topup.service.ServicePlanService;
import com.linksfield.topup.utils.ParameterValidationUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("planService")
@Transactional(rollbackFor = Exception.class)

/**
 * 套餐管理业务逻辑处理实现类
 *
 * @author: lm
 * @date: 2019-11-21 15:40
 */
public class ServicePlanServiceImpl implements ServicePlanService {

    private static Logger logger = Logger.getLogger(ServicePlanServiceImpl.class);

    @Resource
    ServicePlanMapper planMapper;

    @Autowired
    SequenceService sequenceService;

    @Autowired
    CommonUtilsServiceImpl utilsService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 专门用于新建套餐这边的企业列表：
     * <p>
     * 这边的列表要求是都有已启用的方案，符合新建套餐的要求
     * <p>
     * 这个接口可以屏蔽掉没有方案的企业出现在列表里面
     *
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto partnerList(RequestPartnerNameList request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }

        //确认安全，开始验证参数
        if (null == request.getPartnerCode() || StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //去方案列表里面匹配到已经新建过并且有启用方案的企业列表
        List<PartnerConfigContent> partnerList = planMapper.partnerList(request.getPartnerCode());

        //返回的是一个集合：一个List<ResponsePartnerNameList>，ResponsePartnerNameList注明回复的参数有哪些
        List<ResponsePartnerNameList> responseList = new ArrayList<ResponsePartnerNameList>();
        ResponsePartnerNameList response = null;

        for (PartnerConfigContent p : partnerList) {
            response = new ResponsePartnerNameList();
            response.setPartnerCode(p.getPartnerCode());
            response.setPartnerName(p.getPartnerName());
            responseList.add(response);
        }
        //logger.info("专门用于新建套餐：【获取企业名称列表】：" + JSON.toJSONString(responseList));
        return ResultDataDto.addOperationSuccess().setData(responseList);
    }

    /***
     * 除新建套餐选择企业之外的所有：【获取企业名称列表】
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto partnerNameList(RequestPartnerNameList request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }

        //确认安全，开始验证参数
        if (null == request.getPartnerCode() || StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        List<PartnerInfo> partnerList = planMapper.partnerNameList(request.getPartnerCode());

        //返回的是一个集合：一个List<ResponsePartnerNameList>，ResponsePartnerNameList注明回复的参数有哪些
        List<ResponsePartnerNameList> responseList = new ArrayList<ResponsePartnerNameList>();

        for (PartnerInfo p : partnerList) {
            ResponsePartnerNameList response = new ResponsePartnerNameList();
            response.setPartnerCode(p.getPartnerCode());
            response.setPartnerName(p.getPartnerName());
            responseList.add(response);
        }
        //logger.info("除新建套餐选择企业之外的所有：【获取企业名称列表】：" + JSON.toJSONString(responseList));
        return ResultDataDto.addOperationSuccess().setData(responseList);
    }


    /***
     * 新建套餐时：获取套餐列表
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto packageNameList(RequestPackageNameList request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getTradeType()) ||
                StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        List<PartnerPackage> packageList = planMapper.packageNameList(request.getPartnerCode());

        //返回的是一个集合：一个List<ResponsePackageNameList>，ResponsePackageNameList注明回复的参数有哪些
        List<ResponsePackageNameList> responseList = new ArrayList<ResponsePackageNameList>();
        ResponsePackageNameList response = null;

        for (PartnerPackage p : packageList) {
            response = new ResponsePackageNameList();
            response.setPartnerCode(p.getPartnerCode());
            response.setPackageCode(p.getPackageCode());
            response.setPackageName(p.getPackageName());
            responseList.add(response);
        }

        //logger.info("【新建套餐】时：获取套餐列表：" + JSON.toJSONString(responseList));
        return ResultDataDto.addOperationSuccess().setData(responseList);
    }

    /***
     * 原始套餐基本信息:
     *
     * 新建页面选择好组别之后请求获取 右侧基本信息
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto packageBasicInfo(RequestPackageBasicInfo request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);
        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getPackageCode()) || StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }
        List<PartnerPackage> packageList = planMapper.packageListByCode(request.getPartnerCode(), request.getPackageCode());

        ResponsePackageBasicInfo response = null;
        for (PartnerPackage p : packageList) {

            response = new ResponsePackageBasicInfo();
            //企业编码
            response.setPartnerCode(p.getPartnerCode());
            //原始套餐编码
            response.setPackageCode(p.getPackageCode());
            //原始套餐
            response.setPackageName(p.getPackageName());
            //套餐类型
            response.setPackageType(p.getPackageType());
            //原始套餐类型展示
            response.setPackageTypeShow(utilsService.setPackageTypeShow(p.getPackageType()));
            //原始套餐类型的单位
            response.setPackageUnit(utilsService.setPackageUnit(p.getPackageType()));
            //原始套餐可订购范围
            response.setAvailableRange(utilsService.setAvailableRange(p.getPackageType(), p.getMinOrderPeriod(), p.getMaxOrderPeriod()));
            //原始套餐最小可订购周期
            response.setMinOrderPeriod(p.getMinOrderPeriod());
            //原始套餐最大可订购周期
            response.setMaxOrderPeriod(p.getMaxOrderPeriod());
            //覆盖大洲
            response.setContinentName(utilsService.duplicateRemoval(p.getContinentName()));
            //覆盖国家
            response.setCoverCountrys(utilsService.duplicateRemoval(p.getCoverCountrys()));
            //成本价
            if (StringUtils.isNotBlank(p.getCostPrice())) {
                response.setCostPrice(p.getCostPrice());
            } else {
                response.setCostPrice("-");
            }
        }
        logger.info("【新建套餐】时，原始套餐基本信息：" + JSON.toJSONString(response));
        //给前端统一返回
        return ResultDataDto.addOperationSuccess().setData(response);
    }

    /***
     * 确认新建：新建可售卖的套餐服务计划
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto createServicePlan(RequestCreateServicePlan request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);
        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) ||
                StringUtils.isBlank(request.getOperateStatus()) || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getPackageCode()) ||
                StringUtils.isBlank(request.getLanguageType()) || StringUtils.isBlank(request.getServicePlanShowName()) || StringUtils.isBlank(request.getServicePlanShortDesc()) ||
                StringUtils.isBlank(request.getServicePlanDetailsDesc()) || StringUtils.isBlank(request.getPlanCode()) || null == request.getServicePlanSpecAndPrice()) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //先来看看请求创建的套餐名称在当前企业中是否已经存在
        Integer count = planMapper.queryShowName(request.getPartnerCode(), request.getServicePlanShowName());
        if (count > 0) {
            //说明套餐名称重复了，需要马上返回
            logger.info("上架名称重复，不允许创建！");
            return ResultDataDto.addOperationFailure("上架名称重复，不允许创建！");
        }

        //根据企业编码和套餐编码直接数据库查询原始套餐信息，验证同时也是为了拿数据
        List<PartnerPackage> packageList = planMapper.packageListByCode(request.getPartnerCode(), request.getPackageCode());
        String minPeriod = "";
        String maxPeriod = "";
        String packageType = "";
        for (PartnerPackage p1 : packageList) {
            minPeriod = p1.getMinOrderPeriod();
            maxPeriod = p1.getMaxOrderPeriod();
            packageType = p1.getPackageType();
        }
        //验证servicePlanSpecAndPrice规格与价格数组,如果返回的不是success证明有问题就直接返回
        String result = checkSpecAndPriceList(request.getServicePlanSpecAndPrice(), minPeriod, maxPeriod);
        if (!StringOutputConstant.CODE_SUCCESS.equalsIgnoreCase(result)) {
            return ResultDataDto.addOperationFailure(result);
        }

        Integer createCount = 0;
        String servicePlanCode = "";
        for (PartnerPackage p : packageList) {
            servicePlanCode = sequenceService.getSevicePlanCodeNo();
            //查询企业是否存在一个已启用的方案并且匹配出方案编码
            String configType = planMapper.queryConfigTypeByCode(request.getPartnerCode());
            //A:先去写库套餐表：partner_service_plan_t
            createCount = storageServicePlan(p, servicePlanCode, configType, request.getServicePlanShowName(), request.getServicePlanShortDesc(), request.getServicePlanDetailsDesc(),
                    request.getLanguageType(), request.getPlanCode(), request.getOperateStatus());
        }

        //新建套餐服务计划成功
        if (createCount <= 0) {
            //新建套餐时出现失误
            return ResultDataDto.addOperationFailure("新建套餐失败，请稍后重试！");
        }

        Integer specPriceCount = 0;
        for (RequestServicePlanSpecAndPriceList sp : request.getServicePlanSpecAndPrice()) {
            //B:现在去更新新建的套餐表：partner_service_plan_spec_price_t
            specPriceCount = storageServicePlanSpecPrice(sp, request.getPartnerCode(), request.getPackageCode(), packageType, servicePlanCode, request.getServicePlanShowName());
        }
        //小的循环完毕
        //3，准备2个数组，分别允许下一步（1）或者不允许（0）的结果码
        List<String> list3 = new ArrayList<String>();
        List<String> list4 = new ArrayList<String>();

        if (specPriceCount > 0) {
            list3.add("1");
        } else {
            list4.add("0");
        }

        if (!list3.isEmpty() && list4.isEmpty()) {
            //成功的返回
            logger.info("【新建套餐】时，新增成功！");
            return ResultDataDto.addOperationSuccess("新增成功");
        } else {
            logger.info("【新建套餐】时，新增规格价格出现失误！");
            //新建规格价格时出现失误
            return ResultDataDto.addOperationFailure("新增套餐价格失败，请联系管理员！");
        }
    }

    /***
     * 套餐管理【搜索】
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto search(RequestServicePlanSearch request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) ||
                null == request.getPartnerCode() || null == request.getCondition() ||
                null == request.getServicePlanStatus() || null == request.getRp() || null == request.getCurrentPage()) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //1,先去把分页信息拿回来
        long rowCount = planMapper.searchPageCount(request);

        //如果前端未指定查询页码和显示多少行
        Integer rp;
        if (null != request.getRp()) {
            rp = request.getRp();
        } else {
            rp = 10;
        }

        Integer currentPage;
        if (null != request.getCurrentPage()) {
            currentPage = request.getCurrentPage();
        } else {
            currentPage = 1;
        }

        //计算出来的分页的数据
        PageAndSort pageHelper = new PageAndSort();
        pageHelper.setPage(currentPage);
        pageHelper.setRp(rp);
        pageHelper.setRowCount(rowCount);

        //2,计算套餐总数个数，已上架个数，未上架个数
        List<CalculateResultParamVo> countList = planMapper.calculate(request);

        Map<String, Integer> map = new HashMap<String, Integer>();
        for (CalculateResultParamVo vo : countList) {
            map.put(vo.getServicePlanStatus(), vo.getCounts());
        }
        Integer key1 = 0;
        Integer key2 = 0;
        Integer key3 = 0;
        for (String key : map.keySet()) {
            if ("1".equals(key)) {
                key1 = map.get(key);
            }
            if ("2".equals(key)) {
                key2 = map.get(key);
            }
            if ("3".equals(key)) {
                key3 = map.get(key);
            }
        }
        ResponseCalculateCount count = new ResponseCalculateCount();
        count.setTotalCount(key1 + key2 + key3);
        count.setBeenOnCount(key2);
        count.setNotOnCount(key1 + key3);

        //3,拿着请求主体和分页的数据去分页查询套餐列表 (套餐服务计划列表)
        List<PartnerServicePlan> planList = planMapper.search(request, pageHelper);

        List<ResponseServicePlan> servicePlanList = new ArrayList<ResponseServicePlan>();
        ResponseServicePlan servicePlan = null;

        for (PartnerServicePlan sp : planList) {
            servicePlan = new ResponseServicePlan();
            servicePlan.setPartnerCode(sp.getPartnerCode());
            servicePlan.setPartnerName(sp.getPartnerName());
            servicePlan.setServicePlanCode(sp.getServicePlanCode());
            servicePlan.setServicePlanShowName(sp.getServicePlanShowName());
            servicePlan.setPackageFlow(sp.getPackageFlow());
            servicePlan.setCoverCountrys(sp.getCoverCountrys());
            //新建的套餐最小购买周期
            servicePlan.setMinOrderPeriod(setMinOrderPeriod(sp.getPartnerCode(), sp.getPackageCode(), sp.getServicePlanCode(), sp.getPackageType()));
            //servicePlanStatus状态值，用来判断操作内容
            if (StringUtils.isNotBlank(sp.getServicePlanStatus())) {
                servicePlan.setServicePlanStatus(sp.getServicePlanStatus());
            } else {
                servicePlan.setServicePlanStatus("-");
            }
            //statusShow是servicePlanStatus状态值的前端展示
            servicePlan.setStatusShow(setServicePlanStatusShow(sp.getServicePlanStatus()));
            //languageType是套餐语言：0- 汉语;1- 英语;2- 西班牙语;3- 法语4- 葡萄牙语;5- 印度语;6- 德语;7-日语;8- 韩语;9- 阿拉伯语;10-台湾繁中；11-港体繁中
            servicePlan.setLanguageType(setLanguageType(sp.getLanguageType()));
            //更新时间
            try {
                servicePlan.setUpdateDate(sdf.format(s.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sp.getUpdateDate()))));
            } catch (Exception e) {
                logger.debug("时间转换发生了异常！" + e);
            }
            //打包
            servicePlanList.add(servicePlan);
        }
        ResponseSearch response = new ResponseSearch();
        response.setCalculateCount(count);
        response.setServicePlanList(servicePlanList);
        logger.info("套餐管理【搜索】返回：" + JSON.toJSONString(response));
        //成功：给前端统一返回
        return ResultDataDto.addOperationSuccess().setData(response).setPageAndSort(pageHelper);
    }

    /***
     * 套餐管理【编辑】
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto edit(RequestServicePlanEdit request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getServicePlanCode())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //1,去数据库把套餐服务计划基础数据，原始套餐服务数据拿出来
        List<PartnerServicePlan> planList = planMapper.editInfo(request.getPartnerCode(), request.getServicePlanCode());

        ResponseServicePlanInfo planInfo = null;
        ResponsePackageInfo packageInfo = null;

        for (PartnerServicePlan p : planList) {

            planInfo = new ResponseServicePlanInfo();
            planInfo.setServicePlanCode(p.getServicePlanCode());
            planInfo.setServicePlanStatus(p.getServicePlanStatus());
            planInfo.setLanguageType(p.getLanguageType());
            planInfo.setPlanCode(p.getPlanCode());
            planInfo.setServicePlanShowName(p.getServicePlanShowName());
            planInfo.setServicePlanShortDesc(p.getServicePlanShortDesc());
            planInfo.setServicePlanDetailsDesc(p.getServicePlanDetailsDesc());

            packageInfo = new ResponsePackageInfo();
            packageInfo.setPartnerCode(p.getPartnerCode());
            packageInfo.setPackageCode(p.getPackageCode());
            packageInfo.setPackageName(p.getPackageName());
            packageInfo.setPackageType(p.getPackageType());
            //原始套餐类型展示
            packageInfo.setPackageTypeShow(utilsService.setPackageTypeShow(p.getPackageType()));
            //原始套餐类型的单位
            packageInfo.setPackageUnit(utilsService.setPackageUnit(p.getPackageType()));
            //原始套餐可订购范围
            packageInfo.setAvailableRange(utilsService.setAvailableRange(p.getPackageType(), p.getMinOrderPeriod(), p.getMaxOrderPeriod()));
            packageInfo.setMinOrderPeriod(p.getMinOrderPeriod());
            packageInfo.setMaxOrderPeriod(p.getMaxOrderPeriod());
            packageInfo.setContinentName(utilsService.duplicateRemoval(p.getContinentName()));
            packageInfo.setCoverCountrys(utilsService.duplicateRemoval(p.getCoverCountrys()));
            packageInfo.setCostPrice("-");
        }

        //2,去数据库把套餐服务计划的规格与价格拿出来
        List<PartnerServicePlanSpecPrice> spList = planMapper.specPriceInfo(request.getPartnerCode(), request.getServicePlanCode());
        List<ResponseServicePlanSpecPrice> specPriceList = new ArrayList<ResponseServicePlanSpecPrice>();

        for (PartnerServicePlanSpecPrice sp : spList) {
            ResponseServicePlanSpecPrice specPrice = new ResponseServicePlanSpecPrice();
            specPrice.setSpecifications(sp.getSpecifications());
            specPrice.setAutoRenewal(sp.getAutoRenewal());
            specPrice.setOriginalPriceCNY(sp.getOriginalPriceCNY());
            specPrice.setOriginalPriceUSD(sp.getOriginalPriceUSD());
            specPrice.setDiscountPriceCNY(sp.getDiscountPriceCNY());
            specPrice.setDiscountPriceUSD(sp.getDiscountPriceUSD());
            specPriceList.add(specPrice);
        }
        //现在开始封装返回
        ResponseEdit response = new ResponseEdit();
        response.setPackageInfo(packageInfo);
        response.setServicePlanInfo(planInfo);
        response.setSpecPriceList(specPriceList);
        logger.info("套餐管理【编辑】返回：" + JSON.toJSONString(response));
        return ResultDataDto.addOperationSuccess().setData(response);
    }

    /***
     * 在编辑页面：所做的操作：确认新建或者上架
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto editPageOperate(RequestEditPageOperate request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);
        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) ||
                StringUtils.isBlank(request.getOperateStatus()) || StringUtils.isBlank(request.getPartnerCode()) || StringUtils.isBlank(request.getPackageCode()) ||
                StringUtils.isBlank(request.getLanguageType()) || StringUtils.isBlank(request.getServicePlanShowName()) || StringUtils.isBlank(request.getServicePlanShortDesc()) ||
                StringUtils.isBlank(request.getServicePlanDetailsDesc()) || StringUtils.isBlank(request.getServicePlanCode()) || StringUtils.isBlank(request.getPackageType()) ||
                StringUtils.isBlank(request.getPlanCode()) || null == request.getServicePlanSpecAndPrice()) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //根据企业编码和套餐编码直接数据库查询原始套餐信息，验证同时也是为了拿数据
        List<PartnerPackage> packageList = planMapper.packageListByCode(request.getPartnerCode(), request.getPackageCode());
        String minPeriod = "";
        String maxPeriod = "";
        for (PartnerPackage p1 : packageList) {
            minPeriod = p1.getMinOrderPeriod();
            maxPeriod = p1.getMaxOrderPeriod();
        }
        //验证servicePlanSpecAndPrice规格与价格数组,如果返回的不是success证明有问题就直接返回
        String result = checkSpecAndPriceList(request.getServicePlanSpecAndPrice(), minPeriod, maxPeriod);
        if (!StringOutputConstant.CODE_SUCCESS.equalsIgnoreCase(result)) {
            return ResultDataDto.addOperationFailure(result);
        }
        //A:先去清空数据库里面关于当前套餐的价格方案，不管用户有没有做修改，只要操作就必须先去清空
        Integer deleteCount = planMapper.deleteSpecPrice(request.getPartnerCode(), request.getPackageCode(), request.getServicePlanCode());
        //重写价格结果
        Integer createCount = 0;
        //B:删除成功
        if (0 < deleteCount) {
            for (RequestServicePlanSpecAndPriceList sp : request.getServicePlanSpecAndPrice()) {
                //C:现在去更新新建的套餐表：partner_service_plan_spec_price_t
                //重新写价格
                createCount = storageServicePlanSpecPrice(sp, request.getPartnerCode(), request.getPackageCode(), request.getPackageType(), request.getServicePlanCode(), request.getServicePlanShowName());
            }
        } else {
            //删除规格价格方案表出现问题
            return ResultDataDto.addOperationFailure("操作有误，请稍后重试！");
        }
        //3，准备2个数组，分别允许下一步（1）或者不允许（0）的结果码
        List<String> list3 = new ArrayList<String>();
        List<String> list4 = new ArrayList<String>();
        if (createCount > 0) {
            list3.add("1");
        } else {
            list4.add("0");
        }
        //D:规格价格等数据入库成功
        if (!list3.isEmpty() && list4.isEmpty()) {
            //E:去更新当前套餐记录：partner_service_plan_t
            PartnerServicePlan plan = new PartnerServicePlan();
            plan.setPartnerCode(request.getPartnerCode());
            plan.setPackageCode(request.getPackageCode());
            plan.setServicePlanCode(request.getServicePlanCode());
            // :页面操作行为数值刚好与套餐状态的数值相同：用 operateStatus赋值给servicePlanStatus
            plan.setLanguageType(request.getLanguageType());
            plan.setPlanCode(request.getPlanCode());
            plan.setServicePlanShowName(request.getServicePlanShowName());
            plan.setServicePlanShortDesc(request.getServicePlanShortDesc());
            plan.setServicePlanDetailsDesc(request.getServicePlanDetailsDesc());
            plan.setServicePlanStatus(request.getOperateStatus());
            plan.setUpdateDate(Timestamp.valueOf(s.format(new Date())));
            if (StringOutputConstant.CODE_TWO.equals(request.getOperateStatus())) {
                plan.setPutonDate(s.format(new Date()));
            } else {
                plan.setPutonDate("");
            }

            Integer updateCount = planMapper.updateServicePlan(plan);
            //F:套餐列表更新成功
            if (0 < updateCount) {
                //成功的返回
                return ResultDataDto.addOperationSuccess();
            } else {
                //更新套餐服务计划表出现问题
                return ResultDataDto.addOperationFailure("新建套餐失败，请检查参数稍后重试！");
            }
        } else {
            //入库规格价格表出现问题
            return ResultDataDto.addOperationFailure("规格及规格新建失败，请联系管理员！");
        }
    }

    /***
     * 操作：上架，下架，全部上架，全部下架
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto operate(RequestServicePlanOperate request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }

        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) ||
                StringUtils.isBlank(request.getOperateStatus()) || null == request.getOperateList() || request.getOperateList().isEmpty()) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //1，在做批量操作之前先去遍历看看所有的参数是不是都是齐全并且允许操作的

        //2，准备2个数组，分别去添加允许（1）或者不允许（0）的结果码
        List<String> list1 = new ArrayList<String>();
        List<String> list2 = new ArrayList<String>();

        for (RequestServicePlanOperateList o : request.getOperateList()) {

            if (StringUtils.isNotBlank(o.getPartnerCode()) && StringUtils.isNotBlank(o.getServicePlanCode()) && StringUtils.isNotBlank(o.getServicePlanStatus())) {

                if (!StringOutputConstant.CODE_TWO.equals(request.getOperateStatus()) && !StringOutputConstant.CODE_THREE.equals(request.getOperateStatus())) {
                    logger.info("上下架操作指令不正确");
                    return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_ORDER_ERROR);
                }

                //3，去数据库查询上报的套餐的状态：
                String status = planMapper.queryServicePlanStatus(o.getPartnerCode(), o.getServicePlanCode());

                if (status.equals(o.getServicePlanStatus())) {
                    //4，先判断上报的套餐的状态和数据库查询到的是一致的
                    if (StringOutputConstant.CODE_THREE.equals(request.getOperateStatus())) {
                        //5，先判断上发的操作是“下架”
                        if ("2".equals(status)) {
                            list1.add("1");
                        } else {
                            list2.add("0");
                        }
                    } else if (StringOutputConstant.CODE_TWO.equals(request.getOperateStatus())) {
                        //6，再判断上发的操作是“上架”
                        if ("1".equals(status) || "3".equals(status)) {
                            list1.add("1");
                        } else {
                            list2.add("0");
                        }
                    } else {
                        list2.add("0");
                    }
                } else {
                    list2.add("0");
                }

            } else {
                list2.add("0");
            }
        }
        //3，判断上面上面的遍历结果是全部允许（1）还是有存在不允许（0）
        if (!list1.isEmpty() && list2.isEmpty()) {

            for (RequestServicePlanOperateList o : request.getOperateList()) {
                //4，内部开始操作：全部上架，全部下架
                PartnerServicePlan plan = new PartnerServicePlan();
                plan.setServicePlanCode(o.getServicePlanCode());
                plan.setPartnerCode(o.getPartnerCode());
                if (StringOutputConstant.CODE_TWO.equals(request.getOperateStatus())) {
                    plan.setServicePlanStatus(StringOutputConstant.CODE_TWO);
                    plan.setPutonDate(s.format(new Date()));
                    plan.setPutoffDate("");
                } else if (StringOutputConstant.CODE_THREE.equals(request.getOperateStatus())) {
                    plan.setServicePlanStatus(StringOutputConstant.CODE_THREE);
                    plan.setPutonDate("");
                    plan.setPutoffDate(s.format(new Date()));
                }
                planMapper.operateServicePlanStatus(plan);
            }
            //返回成功需要分操作返回
            if (StringOutputConstant.CODE_TWO.equals(request.getOperateStatus())) {
                //成功的返回
                return ResultDataDto.addOperationSuccess("上架成功");
            } else if (StringOutputConstant.CODE_THREE.equals(request.getOperateStatus())) {
                //成功的返回
                return ResultDataDto.addOperationSuccess("下架成功");
            }
            //成功的返回
            return null;
        } else {
            //接口请求中参数可以存在问题
            return ResultDataDto.addOperationFailure("操作有误，请稍后重试！");
        }
    }

    /***
     * 获取企业的语言类型
     * @param request
     * @param token
     * @return
     */
    @Override
    public ResultDataDto partnerLanguageType(RequestPartnerLanguageType request, String token) {

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        //1,先去公共服务类验证token
        String results = utilsService.checkToken(token);

        if (StringUtils.isBlank(results) || !StringOutputConstant.CODE_SUCCESS.equals(results)) {
            // 验证token出现问题，直接返回
            return ResultDataDto.addOperationFailure(results);
        }
        //确认安全，开始验证参数
        if (StringUtils.isBlank(request.getTradeType()) || StringUtils.isBlank(request.getLang()) || StringUtils.isBlank(request.getVersion()) || StringUtils.isBlank(request.getPartnerCode())) {
            // 上报的参数有误
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_PARAM_ERROR);
        }

        //查询企业是否存在一个已启用的方案并且匹配出方案编码
        String planCode = planMapper.queryPlanCodeByCode(request.getPartnerCode());

        if (StringUtils.isBlank(planCode)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_CREATE_ERROR);
        }

        List<PartnerConfigLang> langList = planMapper.queryLangType(request.getPartnerCode(), planCode);

        List<LanguageType> languageList = new ArrayList<LanguageType>();
        LanguageType languageType = null;

        for (PartnerConfigLang lang : langList) {
            languageType = new LanguageType();
            languageType.setLanguageCode(lang.getLanguageCode());
            languageType.setLanguageName(lang.getLanguageName());
            languageList.add(languageType);
        }

        ResponsePartnerLanguageType response = new ResponsePartnerLanguageType();
        response.setPlanCode(planCode);
        response.setLanguageList(languageList);

        logger.info("获取【有已启用方案企业】的语言类型：" + JSON.toJSONString(response));
        // 成功的返回
        return ResultDataDto.addOperationSuccess().setData(response);
    }

    /**
     * 验证servicePlanSpecAndPrice规格与价格数组,
     * 验证中有任何问题都不能继续往下
     * 如果没有问题需要返回success
     * 如果返回的不是success证明有问题就直接返回
     *
     * @param servicePlanSpecAndPrice
     * @param minPeriod
     * @param maxPeriod
     * @return
     */
    private String checkSpecAndPriceList(List<RequestServicePlanSpecAndPriceList> servicePlanSpecAndPrice, String minPeriod, String maxPeriod) {

        String result = "success";
        //2，准备2个数组，分别允许下一步（1）或者不允许（0）的结果码
        List<String> allowList = new ArrayList<String>();
        List<String> notAllowList = new ArrayList<String>();
        List<String> specificationsList = new ArrayList<String>();

        for (RequestServicePlanSpecAndPriceList sp : servicePlanSpecAndPrice) {

            if (StringUtils.isBlank(sp.getSpecifications()) || StringUtils.isBlank(sp.getAutoRenewal()) || StringUtils.isBlank(sp.getDiscountPriceCNY()) ||
                    StringUtils.isBlank(sp.getDiscountPriceUSD()) || null == sp.getOriginalPriceCNY() || null == sp.getOriginalPriceUSD()) {
//            if (StringUtils.isBlank(sp.getSpecifications()) || StringUtils.isBlank(sp.getDiscountPriceCNY()) ||
//                    StringUtils.isBlank(sp.getDiscountPriceUSD()) || null == sp.getOriginalPriceCNY() || null == sp.getOriginalPriceUSD()) {
                notAllowList.add("0");
            } else {
                //将specifications放进集合，为了验证specifications的规格+autoRenewal是否自动续费标志组合有没有重复的
                specificationsList.add(sp.getSpecifications().concat(sp.getAutoRenewal()));
                //specificationsList.add(sp.getSpecifications());
                if (minPeriod.equals(maxPeriod)) {
                    //1,这边要重点验证一下Specifications规格的值，只能是大于1的正整数,当套餐的最小可够数值和最大可够数值相等时，前端传过来的specifications只能=订购周期最小值和最大值；
                    boolean b4 = ParameterValidationUtil.isInteger(sp.getSpecifications()) && sp.getSpecifications().equals(minPeriod) && sp.getSpecifications().equals(maxPeriod);

                    //2，前端传过来的金额也是有限制的
                    boolean b5 = ParameterValidationUtil.isPositive(sp.getDiscountPriceCNY()) && ParameterValidationUtil.isPositive(sp.getDiscountPriceUSD()) &&
                            !"0.0".equals(sp.getDiscountPriceCNY()) && !"0.00".equals(sp.getDiscountPriceCNY()) && !"0.0".equals(sp.getDiscountPriceUSD()) && !"0.00".equals(sp.getDiscountPriceUSD()) &&
                            StringUtils.isBlank(sp.getOriginalPriceUSD()) && StringUtils.isBlank(sp.getOriginalPriceCNY());

                    boolean b6 = StringUtils.isNotBlank(sp.getOriginalPriceUSD()) && StringUtils.isNotBlank(sp.getOriginalPriceCNY()) &&
                            ParameterValidationUtil.isPositive(sp.getDiscountPriceCNY()) && ParameterValidationUtil.isPositive(sp.getDiscountPriceUSD()) &&
                            ParameterValidationUtil.isPositive(sp.getOriginalPriceUSD()) && ParameterValidationUtil.isPositive(sp.getOriginalPriceCNY()) &&
                            !"0.0".equals(sp.getDiscountPriceCNY()) && !"0.00".equals(sp.getDiscountPriceCNY()) && !"0.0".equals(sp.getDiscountPriceUSD()) && !"0.00".equals(sp.getDiscountPriceUSD()) &&
                            !"0.0".equals(sp.getOriginalPriceCNY()) && !"0.00".equals(sp.getOriginalPriceCNY()) && !"0.0".equals(sp.getOriginalPriceUSD()) && !"0.00".equals(sp.getOriginalPriceUSD()) &&
                            0 <= (Double.parseDouble(sp.getOriginalPriceCNY()) - Double.parseDouble(sp.getDiscountPriceCNY())) &&
                            0 <= (Double.parseDouble(sp.getOriginalPriceUSD()) - Double.parseDouble(sp.getDiscountPriceUSD()));

                    boolean b7 = b4 && (b5 || b6);

                    if (b7) {
                        allowList.add("1");
                    } else {
                        notAllowList.add("0");
                        logger.info("新建时：当套餐的最小可够数值和最大可够数值相等时specifications必须等于最小和最大值，或者作为折扣的金额有问题！");
                        result = "输入的价格或者规格不符合规则！";
                    }
                } else {
                    //1，直接验证规格和金额是不是规定值就可以了
                    boolean b4 = ParameterValidationUtil.isInteger(sp.getSpecifications());

                    //2，前端传过来的金额也是有限制的
                    boolean b5 = ParameterValidationUtil.isPositive(sp.getDiscountPriceCNY()) && ParameterValidationUtil.isPositive(sp.getDiscountPriceUSD()) &&
                            !"0.0".equals(sp.getDiscountPriceCNY()) && !"0.00".equals(sp.getDiscountPriceCNY()) && !"0.0".equals(sp.getDiscountPriceUSD()) && !"0.00".equals(sp.getDiscountPriceUSD()) &&
                            StringUtils.isBlank(sp.getOriginalPriceUSD()) && StringUtils.isBlank(sp.getOriginalPriceCNY());

                    boolean b6 = StringUtils.isNotBlank(sp.getOriginalPriceUSD()) && StringUtils.isNotBlank(sp.getOriginalPriceCNY()) &&
                            ParameterValidationUtil.isPositive(sp.getDiscountPriceCNY()) && ParameterValidationUtil.isPositive(sp.getDiscountPriceUSD()) &&
                            ParameterValidationUtil.isPositive(sp.getOriginalPriceUSD()) && ParameterValidationUtil.isPositive(sp.getOriginalPriceCNY()) &&
                            !"0.0".equals(sp.getDiscountPriceCNY()) && !"0.00".equals(sp.getDiscountPriceCNY()) && !"0.0".equals(sp.getDiscountPriceUSD()) && !"0.00".equals(sp.getDiscountPriceUSD()) &&
                            !"0.0".equals(sp.getOriginalPriceCNY()) && !"0.00".equals(sp.getOriginalPriceCNY()) && !"0.0".equals(sp.getOriginalPriceUSD()) && !"0.00".equals(sp.getOriginalPriceUSD()) &&
                            0 <= (Double.parseDouble(sp.getOriginalPriceCNY()) - Double.parseDouble(sp.getDiscountPriceCNY())) &&
                            0 <= (Double.parseDouble(sp.getOriginalPriceUSD()) - Double.parseDouble(sp.getDiscountPriceUSD()));

                    boolean b7 = b4 && (b5 || b6);

                    if (b7) {
                        allowList.add("1");
                    } else {
                        notAllowList.add("0");
                        logger.info("新建时：可够数值是一个范围时specifications必须是正整数，或者作为折扣的金额也必须是大于0的最多2位小数的数字！");
                        result = "输入的价格或者规格不符合规则！";
                    }
                }
            }
        }
        //验证规格和价格数组中有没有不允许为空的参数
        if (allowList.isEmpty() || !notAllowList.isEmpty()) {
            logger.info("servicePlanSpecAndPrice请求参数肯定存在不合法");
            result = "输入的规格或价格不符合规则";
        }
        //接着验证一下规格数组中specifications参数+autoRenewal是否自动续费标志组合有没有重复的
        HashSet<String> set = new HashSet<String>(specificationsList);
        Boolean re = set.size() == specificationsList.size() ? true : false;
        if (!re) {
            logger.info("specifications和autoRenewal组合存在重复的情况");
            result = "规格已存在,不允许重复！";
        }
        return result;
    }

    /**
     * 入库partner_service_plan_t
     *
     * @param p
     * @param servicePlanCode
     * @param configType
     * @return
     */
    private Integer storageServicePlan(PartnerPackage p, String servicePlanCode, String configType, String showName, String shortDesc, String detailsDesc, String languageType, String planCode, String operateStatus) {

        PartnerServicePlan plan = new PartnerServicePlan();
        //1，先将原始套餐中很多基础属性拿到
        plan.setPartnerCode(p.getPartnerCode());
        plan.setPartnerName(p.getPartnerName());
        plan.setPackageCode(p.getPackageCode());
        plan.setPackageName(p.getPackageName());
        plan.setPackageType(p.getPackageType());
        plan.setPackageFlow(p.getPackageFlow());
        plan.setBuyType(p.getBuyType());
        plan.setPriceType(p.getPriceType());
        plan.setMinOrderPeriod(p.getMinOrderPeriod());
        plan.setMaxOrderPeriod(p.getMaxOrderPeriod());
        plan.setCoverCountrys(p.getCoverCountrys());
        plan.setContinentName(p.getContinentName());
        plan.setTags(p.getTags());
        //2，再将接口上发的新的接口的参数拿到 :页面操作行为数值刚好与套餐状态的数值相同：用 operateStatus赋值给servicePlanStatus
        plan.setServicePlanShowName(showName);
        plan.setServicePlanCode(servicePlanCode);
        plan.setServicePlanShortDesc(shortDesc);
        plan.setServicePlanDetailsDesc(detailsDesc);
        plan.setServicePlanStatus(operateStatus);
        plan.setLanguageType(languageType);
        plan.setPlanCode(planCode);
        plan.setConfigType(configType);
        if (StringOutputConstant.CODE_TWO.equals(operateStatus)) {
            plan.setPutonDate(s.format(new Date()));
        } else {
            plan.setPutonDate("");
        }
        return planMapper.createServicePlan(plan);
    }

    /**
     * 入库partner_service_plan_spec_price_t
     *
     * @param sp
     * @return
     */
    private Integer storageServicePlanSpecPrice(RequestServicePlanSpecAndPriceList sp, String partnerCode, String packageCode, String packageType, String servicePlanCode, String showName) {
        PartnerServicePlanSpecPrice specPrice = new PartnerServicePlanSpecPrice();
        specPrice.setPartnerCode(partnerCode);
        specPrice.setPackageCode(packageCode);
        specPrice.setPackageType(packageType);
        specPrice.setServicePlanCode(servicePlanCode);
        specPrice.setServicePlanShowName(showName);
        specPrice.setSpecifications(sp.getSpecifications());
        specPrice.setAutoRenewal(sp.getAutoRenewal());
        specPrice.setOriginalPriceCNY(sp.getOriginalPriceCNY());
        specPrice.setOriginalPriceUSD(sp.getOriginalPriceUSD());
        specPrice.setDiscountPriceCNY(sp.getDiscountPriceCNY());
        specPrice.setDiscountPriceUSD(sp.getDiscountPriceUSD());
        return planMapper.createServicePlanSpecPrice(specPrice);
    }

    /**
     * 找到新建套餐最小的订购周期
     *
     * @param partnerCode
     * @param packageCode
     * @param servicePlanCode
     * @param packageType
     * @return
     */
    private String setMinOrderPeriod(String partnerCode, String packageCode, String servicePlanCode, String packageType) {
        String min = planMapper.queryMinSpecifications(partnerCode, packageCode, servicePlanCode);
        String result = "1";
        if (StringUtils.isNotBlank(min)) {
            if (packageType.equals(StringOutputConstant.CODE_ZERO) || packageType.equals(StringOutputConstant.CODE_ONE)) {
                result = min + "天";
            } else if (packageType.equals(StringOutputConstant.CODE_TWO)) {
                result = min + "个月";
            } else if (packageType.equals(StringOutputConstant.CODE_THREE)) {
                result = String.valueOf(Integer.parseInt(min) * 3) + "个月";
            } else if (packageType.equals(StringOutputConstant.CODE_FOUR)) {
                result = String.valueOf(Integer.parseInt(min) * 6) + "个月";
            } else if (packageType.equals(StringOutputConstant.CODE_SIX)) {
                result = String.valueOf(Integer.parseInt(min) * 4) + "个月";
            } else if (packageType.equals(StringOutputConstant.CODE_SEVEN)) {
                result = String.valueOf(Integer.parseInt(min) * 18) + "个月";
            } else if (packageType.equals(StringOutputConstant.CODE_FIVE)) {
                result = min + "年";
            } else if (packageType.equals(StringOutputConstant.CODE_EIGHT)) {
                result = String.valueOf(Integer.parseInt(min) * 2) + "年";
            }
        }
        return result;
    }

    /**
     * 根据语言类型匹配名称
     *
     * @param languageType
     * @return
     */
    private String setLanguageType(String languageType) {
        //languageType是套餐语言：0- 汉语;1- 英语;2- 西班牙语;3- 法语4- 葡萄牙语;5- 印度语;6- 德语;7-日语;8- 韩语;9- 阿拉伯语;10-台湾繁中；11-港体繁中;12-俄罗斯语
        String result = "-";
        if (StringUtils.isNotBlank(languageType)) {
            if (StringOutputConstant.CODE_ZERO.equals(languageType)) {
                result = "汉语";
            } else if (StringOutputConstant.CODE_ONE.equals(languageType)) {
                result = "英语";
            } else if (StringOutputConstant.CODE_TWO.equals(languageType)) {
                result = "西班牙语";
            } else if (StringOutputConstant.CODE_THREE.equals(languageType)) {
                result = "法语";
            } else if (StringOutputConstant.CODE_FOUR.equals(languageType)) {
                result = "葡萄牙语";
            } else if (StringOutputConstant.CODE_FIVE.equals(languageType)) {
                result = "印度语";
            } else if (StringOutputConstant.CODE_SIX.equals(languageType)) {
                result = "德语";
            } else if (StringOutputConstant.CODE_SEVEN.equals(languageType)) {
                result = "日语";
            } else if (StringOutputConstant.CODE_EIGHT.equals(languageType)) {
                result = "韩语";
            } else if (StringOutputConstant.CODE_NINE.equals(languageType)) {
                result = "阿拉伯语";
            } else if (StringOutputConstant.CODE_TEN.equals(languageType)) {
                result = "台湾繁中";
            } else if (StringOutputConstant.CODE_ELEVEN.equals(languageType)) {
                result = "港体繁中";
            } else if (StringOutputConstant.CODE_TWELVE.equals(languageType)) {
                result = "俄罗斯语";
            }
        }
        return result;
    }

    /**
     * 给套餐类型数字匹配上中文意思
     *
     * @param servicePlanStatus
     * @return
     */
    private String setServicePlanStatusShow(String servicePlanStatus) {
        //statusShow是servicePlanStatus状态值的前端展示
        String result = "-";
        if (StringUtils.isNotBlank(servicePlanStatus)) {
            if (StringOutputConstant.CODE_ONE.equals(servicePlanStatus)) {
                result = "未上架";
            } else if (StringOutputConstant.CODE_TWO.equals(servicePlanStatus)) {
                result = "已上架";
            } else if (StringOutputConstant.CODE_THREE.equals(servicePlanStatus)) {
                result = "未上架";
            }
        }
        return result;
    }


}